package leeon.forpeddy.htmlparse;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import leeon.util.HtmlUtil;

public class StudentHtmlParser {
	
	private List<Map<String, String>> list = new ArrayList<Map<String, String>>();
	
	public StudentHtmlParser(String file) throws IOException {
		//读取
		BufferedReader br = new BufferedReader(new FileReader(file));
		StringBuffer sb = new StringBuffer();
		String s = null;
		while ((s = br.readLine()) != null) {
			sb.append(s);
		}
		br.close();
		String html = sb.toString();
		
		String table = HtmlUtil.findTag(html, "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#111111\" width=\"100%\" id=\"AutoNumber3\">", "</table>");
		String[] trs = HtmlUtil.findTable(table);
		for (int i = 1; i < trs.length ; i ++) {
			Map<String, String> row = new HashMap<String, String>();

//			String[] tds = HtmlUtil.findTrLower(trs[i], "td", 14);
//			row.put("no", tds[1]);
//			row.put("name", tds[2]);
//			row.put("sex", tds[3]);
//			row.put("birthday", tds[4]);
//			row.put("folk", tds[5]);
//			row.put("mobile", tds[7]);
//			row.put("email", tds[8]);
//			row.put("idcard", tds[9]);
//			row.put("address", tds[10]);
//			row.put("cet", tds[11]);
			
//			String[] tds = HtmlUtil.findTrLower(trs[i], "td", 17);
//			row.put("no", tds[1]);
//			row.put("name", tds[3]);
//			row.put("sex", tds[5]);
//			row.put("birthday", tds[6]);
//			row.put("folk", tds[7]);
//			row.put("mobile", tds[9]);
//			row.put("email", tds[10]);
//			row.put("idcard", tds[11]);
//			row.put("address", tds[12]);
//			row.put("cet", tds[13]);

			String[] tds = HtmlUtil.findTrLower(trs[i], "td", 22);
			row.put("no", tds[1]);
			row.put("name", tds[3]);
			row.put("sex", tds[5]);
			row.put("birthday", tds[6]);
			row.put("folk", tds[7]);
			row.put("mobile", tds[9]);
			row.put("email", tds[10]);
			row.put("idcard", tds[11]);
			row.put("address", tds[12]);
			row.put("cet", tds[19]);
			
			
			list.add(row);
		}
	}
	
	public List<Map<String, String>> getResults(String code) {
		for (Map<String, String> row : list) {
			row.put("class", code);
		}
		return list;
	}

	public static void main(String[] args) throws IOException {
		StudentHtmlParser chp = new StudentHtmlParser("D:\\Program Files\\europa\\jee\\workspace\\forpeddy\\misc\\705101012_05春计算机高起专交大班.html");
		System.out.println(chp.getResults("705101012"));
	}

}
